package cn.wjdiankong.levideo.dialog;

import java.io.File;

import com.ss.android.common.applog.GlobalContext;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.data.LevideoData;
import cn.wjdiankong.levideo.database.LocalStorage;
import cn.wjdiankong.levideo.utils.DownloadUtil;
import cn.wjdiankong.levideo.utils.FileUtils;
import cn.wjdiankong.levideo.utils.UIUtil;
import cn.wjdiankong.levideo.utils.Utils;
import cn.wjdiankong.levideo.utils.DownloadUtil.OnDownloadListener;
import cn.wjdiankong.levideo.view.CircleProgressBar;
import cn.wjdiankong.levideo.view.LevideoDialog;

@SuppressLint("HandlerLeak")
public class DownloadVideoDialog {
	
	private LevideoDialog downloadDialog;
	private View parentView;
	private CircleProgressBar progressBar;
	
	private LevideoData videoData;
	private int type;
	private String downloadUrl;
	private String videoUrl;
	
	@SuppressLint("InflateParams")
	public DownloadVideoDialog(Activity act){
		if(parentView == null){
			parentView = LayoutInflater.from(act).inflate(R.layout.dialog_download_progress, null);
		}
		progressBar = (CircleProgressBar) parentView.findViewById(R.id.progress);
		progressBar.setColorSchemeResources(R.color.base_color,R.color.base_color,R.color.base_color);
		downloadDialog = new LevideoDialog(act, parentView, UIUtil.dip2px(act, 160), 0);
		downloadDialog.setCancelable(false);
		downloadDialog.setCanceledOnTouchOutside(false);
	}
	
	public DownloadVideoDialog setVideoData(LevideoData data){
		videoData = data;
		return this;
	}
	
	public DownloadVideoDialog setVideoUrl(String url){
		videoUrl = url;
		return this;
	}
	
	public DownloadVideoDialog setDownloadUrl(String url){
		downloadUrl = url;
		return this;
	}
	
	public DownloadVideoDialog setVideoType(int type){
		this.type = type;
		return this;
	}
	
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler(){
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if(msg.what == 1){
				((CircleProgressBar)msg.obj).setProgress(msg.arg1);
			}else if(msg.what == 2){
				UIUtil.showShortHintToast(GlobalContext.getContext(), "下载完成，设置页面中进行查看");
				String filePath = (String)msg.obj;
				videoData.createTime = System.currentTimeMillis();
				videoData.videoPlayUrl = filePath;
				videoData.type = type;
				LocalStorage.getInstance().saveLevideoData(videoData);
				downloadDialog.dismiss();
			}else if(msg.what == 3){
				UIUtil.showShortHintToast(GlobalContext.getContext(), "下载失败");
				downloadDialog.dismiss();
			}
		}
	};
	
	public void show(){
		if(TextUtils.isEmpty(downloadUrl)){
			downloadUrl = videoUrl;
			UIUtil.showShortHintToast(GlobalContext.getContext(), "下载地址无效");
			return;
		}
		final String filePath = FileUtils.getSaveVideoFileName(GlobalContext.getContext(), type, Utils.getMD5(downloadUrl));
		File pathFile = new File(filePath);
		if(pathFile.exists()){
			UIUtil.showShortHintToast(GlobalContext.getContext(), "文件已经存在");
			return;
		}
		if(!pathFile.getParentFile().exists()){
			pathFile.getParentFile().mkdirs();
		}
		
		DownloadUtil.get().setDownloadUrl(downloadUrl).setDownloadFilePath(pathFile.getAbsolutePath())
			.setDownloadListener(new OnDownloadListener() {
			
			@Override
			public void onDownloading(final int progress) {
				Message msg = new Message();
				msg.what = 1;
				msg.obj = progressBar;
				msg.arg1 = progress;
				mHandler.sendMessage(msg);
			}
			
			@Override
			public void onDownloadSuccess() {
				Message msg = new Message();
				msg.what = 2;
				msg.obj = filePath;
				mHandler.sendMessage(msg);
			}
			
			@Override
			public void onDownloadFailed() {
				mHandler.sendEmptyMessage(3);
			}
		}).download();
		
		downloadDialog.show();
	}

}
